var apitool = require('./apitool.js')
var actmodel = {};
actmodel.getcity = function (hascode) {
  return new Promise((resolve) => {
    actmodel.location().then(function (ret) {
     
      if (ret["status"] == 1) {
        actmodel.positiontocity(ret.position).then(function (xret) {
         
          if (xret["status"] == 1) {
            ret["addr"] = xret["addr"]
            ret["locationaddr"] = xret["locationaddr"]
          } else {
            ret["status"] = -1;
            ret["获取城市失败"];
          }
          resolve(ret);
        })
      } else {
        resolve(ret);
      }
    })
  })
}
actmodel.getallcity = function () {

  },

  actmodel.positiontocity = function (position) {
    return new Promise((resolve) => {
      apitool.get({
        do: 'map_api',
        op: 'locationaddr',
        lat: position.lat,
        lng: position.lng
      }).then(function (addrdata) {
        console.log("position")
        console.log(position)
         console.log("addrdata")
         console.log(addrdata)

        var app = getApp();

      
        var address_component = addrdata.address_component
        
        var locationaddr = {
          lat: position.lat,
          lng: position.lng
        }
        locationaddr["status"] = 1
        var fields = ["province", "city", "district", "street"]
        if (address_component) {
          fields.forEach(item => {
            if (address_component[item]) {
              locationaddr[item] = address_component[item]
            } else {
              locationaddr[item] = ""
            }

          })
        }
        if (addrdata.formatted_addresses) {
          locationaddr["addrinfo"] = addrdata.formatted_addresses["recommend"];
          locationaddr["addrname"] = addrdata.formatted_addresses["rough"];
        }
        if (addrdata.ad_info) {
          locationaddr["adcode"] = addrdata.ad_info["adcode"]
        }
      
        app.globalData.locationaddr = locationaddr;

        resolve({
          status: 1,
          addr: addrdata,
          locationaddr
        });
      })



    })
  }
actmodel.location = function (resend, cache) {
  var app = getApp();
  

  if (resend != false) {
    resend = true;
  }
  if (cache !== false) {
    if (app.globalData.lat) {
      return new Promise((resolve) => {
        resolve({
          status: 1,
          message: "",
          position: {
            lng: app.globalData.lng,
            lat: app.globalData.lat
          }
        })
      })
    }

  }
  return new Promise((resolve) => {

    wx['get'+'Location']({
      type: 'gcj02',
      isHighAccuracy: true,
      success: function (res) {

     
        app.globalData.lat = res.latitude; //
        app.globalData.lng = res.longitude; //把onload定位时候的经纬度存到全局

        resolve({
          status: 1,
          message: "",
          position: {
            lng: res.longitude,
            lat: res.latitude
          }
        })
      },
      fail: function (res) {
 

        //申请确认
        if (resend) {
          wx.getSetting({
            success: (res) => {
              var statu = res.authSetting;
   

              if (!statu['scope.userLocation']) { //没授权
                wx.showModal({
                  title: '获取附近需求授权使用地理位置',
                  content: '获取附近需求授权使用地理位置',
                  success(res) {
                    if (res.confirm) {
                      wx.openSetting({
                        success(res) {
                          if (res.authSetting["scope.userLocation"] == false) {
                            //定位失败
                            resolve({
                              status: -1,
                              message: "定位失败,未授权",
                              position: {
                                lng: -1,
                                lat: -1
                              }
                            })
      
                          } else {
                            actmodel.location(resend).then(function (a) {
                        
                              resolve(a)
                            })
                          }
                        },
                        fail(res) {
      
                          resolve({
                            status: -1,
                            typcode: "",
                            message: "定位失败,未授权",
                            position: {
                              lng: -1,
                              lat: -1
                            }
                          })
                        }
                      });
                    } else if (res.cancel) {
                      resolve({
                        status: -1,
                        typcode: "",
                        message: "定位失败,未授权",
                        position: {
                          lng: -1,
                          lat: -1
                        }
                      })
                    }
                  }
                })
              } else { //已授权
                //做一些事情...

              }
            }
          })
        
        }
      },
    })
  });


}

module.exports = actmodel